package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Targets;
import com.tour.mapper.TargetsMapper;
import com.tour.service.TargetsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2020-12-30
 */
@Service
public class TargetsServiceImpl extends ServiceImpl<TargetsMapper, Targets> implements TargetsService {

    @Autowired
    TargetsMapper targetsMapper;

    public ResponseWrapper addTargets(Targets targets){
        if (targets == null){
            return ResponseWrapper.markError();
        }else {
            int result = targetsMapper.insert(targets);
            if (result == 1){
                return ResponseWrapper.markSuccessButNoData();
            }else {
                return ResponseWrapper.markError();
            }
        }
    }

    public ResponseWrapper findAllTargets(Integer userId,Integer pageSize,Integer pageNum){
        if (userId == null){
            return ResponseWrapper.markError();
        }else {
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> iPage = targetsMapper.findAllTargets(page,userId);
            List list = iPage.getRecords();
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",iPage.getTotal());
            wrapper.getData().put("pageSize",iPage.getSize());
            wrapper.getData().put("pageNum",iPage.getCurrent());
            return wrapper;
        }
    }
}
